//
//  WBN_FM_DetailTableViewCell.m
//  GoodManNews
//
//  Created by 王博男 on 16/1/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "WBN_FM_DetailTableViewCell.h"
#import "Masonry.h"

@interface WBN_FM_DetailTableViewCell ()

@property (nonatomic, assign) NSInteger temp;
@property (nonatomic, assign) NSInteger tempB;

//@property (nonatomic, retain) UIProgressView *progressV;

// 记录点击次数
@property (nonatomic, assign) BOOL cleckCountB;

@end

@implementation WBN_FM_DetailTableViewCell

//- (void)dealloc
//{
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"下载" object:nil];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"结束" object:nil];
//    [super dealloc];
//}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createView];
    }
    return self;
}

- (void)setCount:(NSInteger)count
{
    self.temp = count;
    
    [self createButton];
}

- (void)setTempNum:(NSInteger)tempNum
{
    self.tempB = tempNum;
    
    [self createButtonImage];
}


- (void)layoutSubviews
{
    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.contentView).offset(20);
        make.left.equalTo(self.contentView).offset(20);
        make.bottom.equalTo(self.contentView).offset(-40);
        make.right.equalTo(self.contentView).offset(-60);
        
    }];
    
    
    [self.dateL mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.titleL.mas_bottom);
        make.left.equalTo(self.titleL.mas_left);
        make.height.equalTo(@30);
        make.width.equalTo(@100);
        
    }];
    
    
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.contentView).offset(45);
        make.right.equalTo(self.contentView).offset(-10);
        make.width.equalTo(@50);
        make.height.equalTo(@30);
        
    }];
    
    
//    [self.progressV mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.top.equalTo(self.contentView).offset(80);
//        make.right.equalTo(self.contentView).offset(-10);
//        make.width.equalTo(@50);
//        make.height.equalTo(@10);
//        
//    }];
}

- (void)createView
{
    self.titleL = [UILabel new];
    self.titleL.numberOfLines = 0;
    self.titleL.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    self.titleL.textColor = [UIColor colorWithWhite:0.366 alpha:1.000];
    [self.contentView addSubview:self.titleL];
    
    
    self.dateL = [UILabel new];
    self.dateL.font = [UIFont systemFontOfSize:12];
    self.dateL.textColor = [UIColor colorWithWhite:0.429 alpha:1.000];
    [self.contentView addSubview:self.dateL];
    
    
//    self.progressV = [UIProgressView new];
//    self.progressV.progressViewStyle = UIProgressViewStyleDefault;
//    [self.contentView addSubview:self.progressV];
//    self.progressV.hidden = YES;
    
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:@"下载" object:nil];
//    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction) name:@"结束" object:nil];
}

//- (void)notificationAction:(id)sender
//{
//    CGFloat progress = [[sender object] floatValue];
//    self.progressV.hidden = NO;
//    self.progressV.progress = progress;NSLog(@"%f", self.progressV.progress);
//}
//- (void)notificationAction
//{
//    [self.progressV removeFromSuperview];
//}


- (void)createButton
{
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self.button setTag:(10000 + self.temp)];
    
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.button];
}

- (void)createButtonImage
{
    self.cleckCountB = NO;
    
    if (1 == self.tempB) {
        
        [self.button setTitle:@"已下载" forState:UIControlStateNormal];
        [self.button setTitleColor:[UIColor colorWithWhite:0.588 alpha:1.000] forState:UIControlStateNormal];
        self.button.titleLabel.font = [UIFont systemFontOfSize:12];
        self.button.userInteractionEnabled = NO;
        
    } else if (0 == self.tempB) {
        
        [self.button setImage:[UIImage imageNamed:@"list_FM_myCache_icon_night@3x"] forState:UIControlStateNormal];
        
    } else {
        
        [self.button setTitle:@"缓存中.." forState:UIControlStateNormal];
        [self.button setTitleColor:[UIColor colorWithWhite:0.588 alpha:1.000] forState:UIControlStateNormal];
        self.button.titleLabel.font = [UIFont systemFontOfSize:12];
        
    }
}

- (void)buttonAction:(UIButton *)sender
{
    self.cleckCountB = !self.cleckCountB;
    
    if (YES == self.cleckCountB) {
        
        [self.button setImage:nil forState:UIControlStateNormal];
        [self.button setTitle:@"缓存中.." forState:UIControlStateNormal];
        [self.button setTitleColor:[UIColor colorWithWhite:0.588 alpha:1.000] forState:UIControlStateNormal];
        self.button.titleLabel.font = [UIFont systemFontOfSize:12];
        
        [self.delegate downLoadFMWith:(sender.tag - 10000) bol:YES];
        
    } else {
        
        [self.button setTitle:nil forState:UIControlStateNormal];
        [self.button setImage:[UIImage imageNamed:@"list_FM_myCache_icon_night@3x"] forState:UIControlStateNormal];
        [self.button setTitleColor:[UIColor colorWithWhite:0.588 alpha:1.000] forState:UIControlStateNormal];
        self.button.titleLabel.font = [UIFont systemFontOfSize:12];
        
        [self.delegate downLoadFMWith:(sender.tag - 10000) bol:NO];
        
    }
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
